import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import copy from "rollup-plugin-copy";
import commonjsBuild from "@rollup/plugin-commonjs";
import commonjsServe from "vite-plugin-commonjs";
// import libCss from "vite-plugin-libcss";
// console.log(path);

export default defineConfig({
  base: "./",
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "src")
    },
    extensions: [".js", ".vue", ".json", ".ts"] // 使用路径别名时想要省略的后缀名，可以自己 增减
  },
  build: {
    outDir: "dist/legacy",
    // assetsDir: "./",
    cssCodeSplit: true,
    lib: {
      entry: path.resolve(__dirname, "src/main.js"),
      name: "index",
      fileName: "index"
      // cssCodeSplit: false
      // fileName: (format) => `visual-sdk/VisualAppLib.${format}.js`
    },
    rollupOptions: {
      // external: ["vue", "vue-router", "element-plus"],//需要排除的依赖
      output: {
        assetFileNames: (assetInfo) => {
          console.log("assetInfo", assetInfo);
          return "[name][extname]";
        },
        // 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
        globals: { vue: "Vue", "element-plus": "ElementPlus" }
      }
    }
  },
  plugins: [
    vue(),
    // libCss(), // 使用`会umd.js中会出现bug
    // copy({
    //   targets: [
    //     { src: "./src.app.sdk/*", dest: "./dist/visual-sdk" } //执行拷贝
    //     // {
    //     //   src: "dist/src.app.sdk/AppProvider.css",
    //     //   dest: "dist",
    //     //   rename: "VisualAppLib.css"
    //     // }
    //   ],
    //   hook: "writeBundle"
    // }),
    commonjsServe(),
    commonjsBuild()
  ]
});
